package top.huntwolf.admin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.huntwolf.admin.common.enums.BookConditionEnum;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.dao.BookDao;
import top.huntwolf.admin.dao.BookReserveDao;
import top.huntwolf.admin.exception.ServiceException;
import top.huntwolf.admin.pojo.BookReserve;
import top.huntwolf.admin.pojo.orderEntity.Order;
import top.huntwolf.admin.pojo.orderEntity.OrderBook;
import top.huntwolf.admin.pojo.book.BuyBook;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BookReserveService {

    @Autowired
    private BookReserveDao reserveDao;

    public BookReserve select(String condition){
        BookReserve bookCondition = reserveDao.select(BookConditionEnum.getConditionByDesc(condition));
        if(bookCondition ==null){
            throw new ServiceException(CodeMsg.NOT_DISCOUNT.fillArgs("该品相不存在"));
        }
        return bookCondition;
    }

    public void delete(String isbn){
        reserveDao.delete(isbn);
    }

    public void insert(BookReserve bookCondition){
        reserveDao.insert(bookCondition);
    }

    public void replace(BookReserve bookCondition) {
        reserveDao.replace(bookCondition);}

    public void update(BookReserve bookCondition){

        reserveDao.update(bookCondition);
    }

    public List<BookReserve> selectAll(){
        return reserveDao.selectAll();
    }


    public void addBookReserve(BuyBook book,int storage) {

        List<BookReserve> reserveList = book.getBookReserves();
        List<BookReserve> bookReserves = new ArrayList<>();
        Date date = new Date();
        for (BookReserve bookReserve : reserveList){
            bookReserve.setStorage(storage);
            bookReserve.setIsbn(book.getIsbn());
            BookReserve reserve = reserveDao.selectBook(book.getIsbn(),bookReserve.getConditionEnum());
            if(reserve!=null){
                reserve.setUpdateTime(date);
                reserve.setStorage(bookReserve.getStorage());
                reserveDao.increaseStorage(reserve);
                continue;
            }
            //TODO 逻辑不完善
            if(bookReserve.getDiscount()==null){
                bookReserve.setRealPrice(book.getPrice());
            }else {
                DecimalFormat df = new DecimalFormat("#.00");
                bookReserve.setRealPrice(String.valueOf(df.format(Double.parseDouble(book.getPrice())*Double.parseDouble(bookReserve.getDiscount()))));
            }
            bookReserve.setUpdateTime(date).setCreateTime(date);
            bookReserves.add(bookReserve);

        }
        if (bookReserves.size()>0){
            reserveDao.replaceReserve(bookReserves);
        }
    }

    public void checkoutStorage(List<OrderBook> books){

        for (OrderBook orderBook : books){
            BookReserve bookReserve = reserveDao.selectBook(orderBook.getIsbn(), BookConditionEnum.MEDIUM);
            if (bookReserve.getStorage()<orderBook.getNum()){
                throw new ServiceException(CodeMsg.ITEM_EXCEED.fillArgs(orderBook.getIsbn()));
            }
        }
    }

    public void submitOrderReduceStorage(List<OrderBook> books){
        for (OrderBook orderBook : books){
            BookReserve bookReserve = reserveDao.selectBook(orderBook.getIsbn(), orderBook.getConditionEnum());
            if(bookReserve.getStorage() - orderBook.getNum()<0){
                throw new ServiceException(CodeMsg.ITEM_EXCEED.fillArgs(orderBook.getIsbn()));
            }
            bookReserve.setStorage(orderBook.getNum());
            bookReserve.setUpdateTime(new Date());
            reserveDao.reduceStorage(bookReserve);
        }
    }

    public void cancelOrderIncreaseStorage(List<OrderBook> books){
        for (OrderBook orderBook : books) {
            BookReserve bookReserve = reserveDao.selectBook(orderBook.getIsbn(), orderBook.getConditionEnum());
            bookReserve.setStorage(orderBook.getNum());
            bookReserve.setUpdateTime(new Date());
            reserveDao.increaseStorage(bookReserve);
        }
    }

    public void incompleteOrderIncreaseStorage(Order order){
        List<OrderBook> books = order.getBooks();
        for (OrderBook orderBook : books) {
            BookReserve bookReserve = reserveDao.selectBookByStore(orderBook.getIsbn(), orderBook.getConditionEnum(),order.getStoreId());
            bookReserve.setStorage(orderBook.getNum());
            bookReserve.setUpdateTime(new Date());
            reserveDao.increaseStorage(bookReserve);
        }
    }

}
